package test07;

import java.util.*; /**
 * 质量评估处理器
 */
public class QualityHandler extends ContentHandler {
    private double qualityThreshold = 0.6;
    private Set<String> repetitionPatterns;
    private Map<String, Double> coherenceMetrics;

    public QualityHandler() {
        super("质量评估器");
        initializePatterns();
    }

    private void initializePatterns() {
        repetitionPatterns = new HashSet<>(Arrays.asList(
                "重复重复重复", "一样的文本", "循环生成"
        ));

        coherenceMetrics = new HashMap<>();
        coherenceMetrics.put("逻辑连贯性", 0.8);
        coherenceMetrics.put("语法正确性", 0.9);
        coherenceMetrics.put("信息完整性", 0.7);
    }

    @Override
    protected boolean canHandle(ContentCheckRequest request) {
        // 主要处理文本和多模态内容
        return request.getContentType() == ContentType.TEXT ||
                request.getContentType() == ContentType.MULTIMODAL;
    }

    @Override
    protected CheckResult doCheck(ContentCheckRequest request) {
        String content = request.getContentData().toString();

        // 检查重复模式
        for (String pattern : repetitionPatterns) {
            if (content.contains(pattern)) {
                return new CheckResult(true, RiskLevel.LOW,
                        "检测到低质量内容（重复模式）", 0.75, "PASS"); // 不阻断，只标记
            }
        }

        // 模拟质量评分
        double qualityScore = calculateQualityScore(content);

        if (qualityScore < qualityThreshold) {
            return new CheckResult(true, RiskLevel.LOW,
                    "内容质量较低，评分: " + String.format("%.2f", qualityScore),
                    qualityScore, "PASS"); // 不阻断传递
        }

        return new CheckResult(true, RiskLevel.LOW,
                "内容质量良好，评分: " + String.format("%.2f", qualityScore),
                qualityScore, "PASS");
    }

    private double calculateQualityScore(String content) {
        // 模拟质量评分计算
        return 0.5 + Math.random() * 0.5;
    }

    public void setQualityThreshold(double threshold) {
        this.qualityThreshold = threshold;
    }
}
